import * as types from './mutation-types'
import Vue from 'vue'
import {getCurrentDateTime, deepClone} from '@/utils/m7Utils'
import m7dicMap from '@utils/m7dicMap'
export default {
  [types.REFRESH_BUSINESS_UNDEALNUM] (state, data) {
    Vue.set(state, 'roleNum', data.roleNum)
    Vue.set(state, 'total', data.total)
    Vue.set(state, 'noReadNum', data.noReadNum)
  },
  [types.GET_ROLE_UNDEAL_BUSINESS] (state, data) {
    Vue.set(state, 'businessList', data)
  },
  [types.GET_UNDEAL_BUSINESS] (state, data) {
    Vue.set(state, 'businessList', data)
  },
  [types.GET_ASSIGNED_BUSINESS] (state, data) {
    Vue.set(state, 'businessList', data)
  },
  [types.GET_FOLLOWED_BUSINESS] (state, data) {
    let list = data.list
    list.forEach((item) => {
      item.followedAgents = item.followedAgents || []
    })
    Vue.set(state, 'businessList', data)
  },
  [types.SET_BUSSINESS_MORE] (state, {more}) {
    state.businessList.more = more
    // state.callList[activeName].count = count
  },
  [types.QUERY_ALL_BUSINESS] (state, data) {
    let list = data.list
    list.forEach((item) => {
      item.followedAgents = item.followedAgents || []
    })
    Vue.set(state, 'businessList', data)
  },
  [types.GET_ALL_UNCOMPLETE_BUSINESS] (state, data) {
    Vue.set(state, 'businessList', data)
  },
  [types.GET_ALL_FINISHED_BUSINESS] (state, data) {
    Vue.set(state, 'businessList', data)
  },
  [types.GET_BUSINESS_DETAIL_BY_ID] (state, {data, tabType}) {
    Vue.set(state.current, 'businessInfo', data)
    // if (tabType === 'business_all') {
    //  state.businessList.list.forEach((item, index) => {
    //    if (item._id === data._id) {
    //      Vue.set(state.businessList.list, index, data)
    //    }
    //  })
    // }
  },
  [types.REMOVE_BUSINESS] (state, id) {
    state.businessList.list.forEach((item, index) => {
      if (item._id === id) {
        state.businessList.list.splice(index, 1)
        state.businessList.count = state.businessList.count - 1
      }
    })
  },
  [types.DELETE_BUSINESS] (state, data) {
    state.success = data
  },
  [types.GET_CUSTOMER_HISTORY_BUSINESS] (state, {data, type}) {
    Vue.set(state.current[type], 'businessHistory', data)
  },
  [types.EXPORT_BUSINESS] (state, {data, type}) {
    Vue.set(state.current[type], 'exportPath', data)
  },
  [types.SET_BUSINESS_FLOWS] (state, flows) {
    console.log(flows);
    let data = []
    flows.forEach((item) => {
      data.push({value: item._id, label: item.name, condition: {flow: item._id}})
    })
    Vue.set(state.transCache, 'businessTypes', data)
  },
  [types.ADD_BUSINESS_ACTION_HISTORY] (state, data) {
    let newHistory = state.current.businessAction[data.data.businessId]['history']
    if (newHistory) {
      newHistory.unshift(data.data)
      Vue.set(state.current.businessAction[data.data.businessId], 'history', newHistory)
    }
  },
  [types.SET_BUSINESS_ACTION] (state, data) {
    Vue.set(state.current.businessAction, data._id, data)
  },
  [types.REFRESH_CUSTOMER_BUSINESS_HISTORY] (state, data) {
    Vue.set(state.transCache, 'refreshCustomerBusinessHistory', {random: Math.random(), customer: data})
  },
  [types.SET_BUSINESS_CONDITION] (state, data) {
    Vue.set(state, 'businessCondition', data)
  },
  [types.CHANGE_BUSINESS_MASTER] (state, data) {
    if (state.current.businessAction[data._id]) {
      let business = state.current.businessAction[data._id]
      business.master = data.master
      Vue.set(state.current.businessAction, data._id, business)
    }
    // state.businessList.list.forEach((item, index) => {
    //  if (item._id === data._id) {
    //    Vue.set(state.businessList.list[index], 'master', data.value)
    //  }
    // })
  },
  [types.REFRESH_LEFT] (state, data) {
    Vue.set(state.transCache, 'refreshLeft', data)
  },
  [types.REFRESH_BUS_RIGHT] (state, data) {
    Vue.set(state.transCache, 'refreshBusRight', data)
  },
  [types.SUMMATY_LOADED] (state, data) {
    Vue.set(state.transCache, 'summaryLoaded', data)
  },
  [types.CUSTOM_SEARCH] (state, data) {
    Vue.set(state.transCache, 'customSearch', Math.random())
  },
  [types.CHANGE_READ_STATUS] (state, data) {
    state.businessList.list.forEach((item, index) => {
      if (item._id === data) {
        state.businessList.list[index].isReadFlag = true
        Vue.set(state.businessList, 'list', state.businessList.list)
      }
    })
  },
  [types.CLEAR_BUS_LIST] (state) {
    Vue.set(state, 'businessList', {list: [], count: 0})
  },
  // 更新工单卡片信息
  [types.UPDATE_BUS_CARD] (state, data) {
    let list = state.businessList.list || []
    list.some((item, index) => {
      if (item._id === data._id) {
        for (let i in data) {
          Vue.set(list[index], i, data[i])
        }
        return true
      }
    })
  },
  // 更新工单信息
  [types.UPDATE_BUS_DETAIL] (state, data) {
    let business = state.current.businessAction || {}
    if (business[data._id]) {
      for (let i in data) {
        Vue.set(business[data._id], i, data[i])
      }
    }
  },
  // 删除工单缓存
  [types.DELETE_BUS_CACHE] (state, cacheId) {
    let busCache = m7dicMap.getCache('busCache')
    delete busCache[cacheId]
  },
  [types.FOLLOWED_SETTING] (state, data) {
    let list = state.businessList.list || []
    let business = state.current.businessAction || {}
    let curretnBusiness = business[data._id] || {}
    let followedAgents = curretnBusiness.followedAgents || []
    let beforeFollowedAgents = deepClone(followedAgents)
    if (data.type === 'follow') {
      // 主动关注
      followedAgents.push(data.agents)
    } else if (data.type === 'unfollow') {
      // 取消关注
      followedAgents.some((item, index, arr) => {
        if (item === data.agents) {
          followedAgents.splice(index, 1)
          return true
        }
      })
    } else {
      // 被动关注
      followedAgents = deepClone(data.agents)
    }
    list.forEach((item) => {
      if (item._id === data._id) {
        item.followedAgents = followedAgents
        Vue.set(state.businessList, 'list', list)
      }
    })
    curretnBusiness.followedAgents = followedAgents
    curretnBusiness.history.unshift({action: 'share', step: data.step, master: data.master, excuteData: {beforeFollowedAgents: beforeFollowedAgents, currentFollowedAgents: deepClone(followedAgents)}, time: getCurrentDateTime()})
    Vue.set(state.current.businessAction, data._id, curretnBusiness)
    Vue.set(state.current.businessAction[data._id], 'history', curretnBusiness.history)
  },
  // 催办
  [types.CUIBAN] (state, data) {
    let business = state.current.businessAction || {}
    let curretnBusiness = business[data._id] || {}
    Vue.set(curretnBusiness, 'urgeTime', data.urgeTime)
    curretnBusiness.history.unshift({action: 'urge', master: data.master, excuteData: {urgeContent: data.urgeContent}, time: getCurrentDateTime()})
    Vue.set(state.current.businessAction[data._id], 'history', curretnBusiness.history)
  },
  [types.GET_FIRST_BUSINESS_QUERY] (state, query) {
    Vue.set(state, 'getBusinessFirstQuery', query)
  },
  [types.GET_BUSINESS_QUERY] (state, query) {
    Vue.set(state, 'getBusinessQuery', query)
  },
  [types.SET_BUSINESS_TYPE] (state, type) {
    Vue.set(state, 'currentBusinessType', type)
  },
  [types.SET_BUSINESS_IS_SHOW_BOX] (state, data) {
    state.showNewBusinessRouterBox = data.isShow
    if (data.isRefresh) {
      state.isRefreshBusinessView = Math.random()
    }
  },
  [types.OPERATION_NEW_BUSINESS_SLIDE] (state, data) {
    Vue.set(state, 'operationNewBusinessObj', data)
  },
  [types.SET_BUSINESS_SLIDE_LIST] (state, data) {
    state.businessSlideListRefresh = Math.random()
  },
  [types.SET_BUSINESS_SHOW_LOADING] (state, isShow) {
    state.showLoading = isShow
  },
  [types.SET_IS_NOEDITING] (state, isShow) {
    state.isNoediting = isShow
  },
  [types.REFRESH_MENUBAR] (state, item) {
    state.refreshMenuBar = item
  }
}
